package com.study.mybatis;

import com.study.mybatis.dao.UserDao;
import com.study.mybatis.model.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.Reader;
import java.sql.SQLException;
import java.util.*;

public class Test5 {

    private static SqlSessionFactory sqlSessionFactory;
    private static Reader reader;

    static {
        try {
            reader = Resources.getResourceAsReader("Configure.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static SqlSessionFactory getSession() {
        return sqlSessionFactory;
    }

    public static void main(String[] args) {
        SqlSession session = getSession().openSession();
        // Configure  mapper
        session.getConfiguration().addMapper(UserDao.class);

        UserDao userDaoImpl = session.getMapper(UserDao.class);
//        Map<String, String> paramMap = new HashMap<>();
//        paramMap.put("username", "zhangsan");
//        paramMap.put("password", "1");
//        int result = userDaoImpl.finOneBtUserNameAndPwd(paramMap);
//        System.out.println("查询记录数：" + result);
//
//        List<User> userList = userDaoImpl.findAll();
//        System.out.println(userList);
//
//        User user = userDaoImpl.finOneByUserName("wangzhen");
//        System.out.println("one user " + user);
//
//        // 插入用户
//        User insertUser = new User();
//        int now = (int)(System.currentTimeMillis()/1000);
//        insertUser.setCreateTime(now);
//        insertUser.setUpdateTime(now);
//        insertUser.setUserGid(UUID.randomUUID().toString());
//        insertUser.setUsername("zhangwu");
//        insertUser.setPassword("1");
//        int insertResult = userDaoImpl.insert(insertUser);
//        System.out.println("添加用户结果" + insertResult);
//        session.commit();

        List<User> userList1 = userDaoImpl.findBtIds(Arrays.asList(1,6));
        System.out.println(userList1);
    }
}
